package com.zhsj.OkeHomeConsumption.ui.activity.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.OkeHomeConsumptionApi;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.network.HttpClient;
import com.zhsj.OkeHomeConsumption.network.JsonResponseHandler;
import com.zhsj.OkeHomeConsumption.other.TimeChecker;
import com.zhsj.OkeHomeConsumption.other.URLEntity;
import com.zhsj.OkeHomeConsumption.utils.LogUtils;
import com.zhsj.OkeHomeConsumption.utils.SignUtil;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;

/**
 * Created by zwj on 2018/3/13.
 * 预计收入
 */

public class MoneyExpectFragment extends BaseFragment implements View.OnClickListener{

    /**
     * 计时器
     */
    private TimeChecker timeChecker = null;
    /**
     * 实例化ProgressDrawableAlertDialog
     */
    private ProgressDrawableAlertDialog pDialogUtils = null;

    //左标提
    private TextView topbar_textview_leftitle;
    //标题
    private TextView topbar_textview_title;

    //项目预计收入
    private LinearLayout project_expect_money_linear;
    private TextView project_expect_money_text;
    //邀请预计收入
    private LinearLayout invite_expect_money_linear;
    private TextView invite_expect_money_text;
    //超额预计收入
    private LinearLayout excess_expect_money_linear;
    private TextView excess_expect_money_text;

    //预计收入说明
    private TextView instructions_text;

    private FormalUserInfo userInfo;

    public static MoneyExpectFragment newInstance(FormalUserInfo userInfo) {
        Bundle args = new Bundle();
        MoneyExpectFragment fragment = new MoneyExpectFragment();
        args.putSerializable("userInfo",userInfo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userInfo = (FormalUserInfo) getArguments().getSerializable("userInfo");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_expect, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        exoectMoney(userInfo.getId() + "");
    }

    private void initView(View view){
        timeChecker = new TimeChecker(TimeOutHandler.timeOutHandler, Constants.TIME_OUT);
        pDialogUtils = new ProgressDrawableAlertDialog(_mActivity);
        topbar_textview_leftitle = (TextView) view.findViewById(R.id.topbar_textview_leftitle);
        topbar_textview_leftitle.setVisibility(View.VISIBLE);
        topbar_textview_leftitle.setOnClickListener(this);
        topbar_textview_title = (TextView) view.findViewById(R.id.topbar_textview_title);
        topbar_textview_title.setText("预计收入");
        project_expect_money_linear = (LinearLayout) view.findViewById(R.id.project_expect_money_linear);
        project_expect_money_linear.setOnClickListener(this);
        project_expect_money_text = (TextView) view.findViewById(R.id.project_expect_money_text);
        invite_expect_money_linear = (LinearLayout) view.findViewById(R.id.invite_expect_money_linear);
        invite_expect_money_linear.setOnClickListener(this);
        invite_expect_money_text = (TextView) view.findViewById(R.id.invite_expect_money_text);
        excess_expect_money_linear = (LinearLayout) view.findViewById(R.id.excess_expect_money_linear);
        excess_expect_money_linear.setOnClickListener(this);
        excess_expect_money_text = (TextView) view.findViewById(R.id.excess_expect_money_text);
        instructions_text = (TextView) view.findViewById(R.id.instructions_text);
        instructions_text.setOnClickListener(this);
    }

    public void exoectMoney(final String userId) {
        // post传参使用的HashMap
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        // 生成sign值所使用的HashMap
        HashMap<String, String> mSendCodeHashMap = new HashMap<String, String>();
//        mSendCodeHashMap.put("token", Constants.TOKEN);
        mSendCodeHashMap.put("v", Constants.APPVERSION);
        mSendCodeHashMap.put("t", Constants.TIME);
        mSendCodeHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mSendCodeHashMap.put("platform", Constants.PLAFORM);
        mSendCodeHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mSendCodeHashMap.put("client", Constants.CLIENT);
        mSendCodeHashMap.put("userId", userId);

        //post 传参
        paramMaps.put("userId", userId);

        // 网络计时器及线程
        TimeOutHandler.pDialogUtils = pDialogUtils;
        pDialogUtils.show();
        timeChecker.start();
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mSendCodeHashMap);

        HttpClient.post(OkeHomeConsumptionApi.EXPECT_MONEY, paramMaps, new JsonResponseHandler() {

            @Override
            public void onSuccess(int statusCode, String content) {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, content);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
                try {
                    LogUtils.e("content_预计收入", "content=" + content);
                    JSONTokener jsonT = new JSONTokener(content);
                    JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
                    String code = jsonOBJ.optString("code");
                    String message = jsonOBJ.optString("message");
                    if (code.equals("N000000")) {
                        // 数据对接正常
                        JSONObject jsonObject = jsonOBJ.optJSONObject("data");
                        project_expect_money_text.setText("¥" + jsonObject.optString("commission"));
                        invite_expect_money_text.setText("¥" + jsonObject.optString("inviting"));
                        excess_expect_money_text.setText("¥" + jsonObject.optString("excess"));
                    } else if (code.equals(Constants.STATUSTOKENERROR)) {
                        if (!message.equals("null")) {
                            showShortToast(message);
                        }
                    } else {
                        if (!message.equals("null")) {
                            showShortToast(message);
                        }
                    }
                } catch (Exception ex) {
                    LogUtils.e("DresserPartnerRegisterPost", "失败 ");
                }
            }

            @Override

            public void onFailure(Throwable e) {
                // TODO Auto-generated method stub
                super.onFailure(e);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
                LogUtils.e("onFailure", "" + e.toString());
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.topbar_textview_leftitle:
                _mActivity.onBackPressed();
                break;
            case R.id.project_expect_money_linear:
                //项目预计收入
                start(ProjectExpectMoneyFragment.newInstance(userInfo));
                break;
            case R.id.invite_expect_money_linear:
                //邀请预计收入
                start(InviteExpectMoneyFragment.newInstance(userInfo));
                break;
            case R.id.excess_expect_money_linear:
                //超额预计收入
                start(ExcessExpectMoneyFragment.newInstance(userInfo));
                break;
            case R.id.instructions_text:
                //预计收入说明
                start(InstructionsFragment.newInstance());
                break;
        }
    }
}
